package com.linkage.jz.bankruptcy.activity.Vote;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.linkage.jz.bankruptcy.R;
import com.linkage.jz.bankruptcy.base.BaseCreateActivity;
import com.linkage.jz.bankruptcy.bean.Vote.ProVote;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteDetail;
import com.linkage.jz.bankruptcy.bean.Vote.ProVoteResult;
import com.linkage.jz.bankruptcy.event.RefreshEvent;
import com.linkage.jz.bankruptcy.net.BaseSubscriber;
import com.linkage.jz.bankruptcy.net.Network;
import com.linkage.jz.bankruptcy.net.ToastUtil;
import com.linkage.jz.bankruptcy.widget.CommonPromptDialog;
import com.linkage.jz.bankruptcy.widget.VoteOverviewPromptDialog;
import com.linkage.jz.bankruptcy.widget.datepicker.DateFormatUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;

import java.text.NumberFormat;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author AFAP
 * 表决发布结果
 */
public class ProVoteCreateResultActivity extends BaseCreateActivity implements View.OnClickListener {
    public static final String KEY_VOTE = "vote";
    public static final String KEY_RESULT = "KEY_RESULT";
    public static final String KEY_RECREATE = "KEY_RECREATE";


    private ProVoteDetail mProVoteDetail;
    private boolean isReCreate = false;

    private EditText et_notice;
    private CheckBox ck_voteName, ck_voteType, ck_voteContent, ck_voteRule, ck_voteRuleExpls, ck_voteTime,
            ck_voteDataSta, ck_voteResult, ck_voteMember_1, ck_voteMember_2;
    private TextView tv_all_check;

    private String voteMember = "true";
    private ProVoteResult mProVoteResult;
    private CommonPromptDialog mPromptDialog;

    @Override
    public void initData() {
    }

    @Override
    public void setListener() {

    }

    @Override
    public void initTitle() {

    }

    @Override
    public void initView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_pro_vote_create_result);
        super.onCreate(savedInstanceState);
        titlebar_title.setText("表决结果文件设置");

        mProVoteDetail = (ProVoteDetail) getIntent().getSerializableExtra(KEY_VOTE);
        isReCreate = getIntent().getBooleanExtra(KEY_RECREATE, false);

        if (isReCreate) {
            mProVoteResult = (ProVoteResult) getIntent().getSerializableExtra(KEY_RESULT);
            if (mProVoteDetail.getResultStatus() == 1) {
                TextView tv_tips = findViewById(R.id.tips);
                tv_tips.setText("提示：生成文件仅供本次需要临时保存，不影响公示结果");
            }
        }
        initViews();
    }

    @Override
    public void onBackPressed() {
        //super.onBackPressed();
        mPromptDialog = new CommonPromptDialog(this, CommonPromptDialog.TYPE_TIPS);
        mPromptDialog.setContent("放弃编辑？");
        mPromptDialog.setPositiveClickListener("是", view -> ProVoteCreateResultActivity.super.onBackPressed());
        mPromptDialog.setNegativeClickListener("否", view -> mPromptDialog.dismiss());
        mPromptDialog.show();
    }

    private CompoundButton.OnCheckedChangeListener comListener = (buttonView, isChecked) -> changeAllCheckStatus();

    private void changeAllCheckStatus() {
        boolean isAll =
                ck_voteName.isChecked() && ck_voteType.isChecked() && ck_voteContent.isChecked() && ck_voteRule.isChecked()
                        && ck_voteRuleExpls.isChecked() && ck_voteTime.isChecked() && ck_voteDataSta.isChecked() && ck_voteResult.isChecked()
                        && !TextUtils.isEmpty(voteMember);

        if (isAll) {
            tv_all_check.setText("取消全选");
        } else {
            tv_all_check.setText("全选");
        }
    }


    public void initViews() {
        // 特殊处理下，
        titlebar_back.setOnClickListener(view -> onBackPressed());

        tv_all_check = findViewById(R.id.tv_all_check);

        ck_voteName = findViewById(R.id.ck_voteName);
        ck_voteType = findViewById(R.id.ck_voteType);
        ck_voteContent = findViewById(R.id.ck_voteContent);
        ck_voteRule = findViewById(R.id.ck_voteRule);
        ck_voteRuleExpls = findViewById(R.id.ck_voteRuleExpls);
        ck_voteTime = findViewById(R.id.ck_voteTime);
        ck_voteDataSta = findViewById(R.id.ck_voteDataSta);
        ck_voteResult = findViewById(R.id.ck_voteResult);

        ck_voteName.setOnCheckedChangeListener(comListener);
        ck_voteType.setOnCheckedChangeListener(comListener);
        ck_voteContent.setOnCheckedChangeListener(comListener);
        ck_voteRule.setOnCheckedChangeListener(comListener);
        ck_voteRuleExpls.setOnCheckedChangeListener(comListener);
        ck_voteTime.setOnCheckedChangeListener(comListener);
        ck_voteDataSta.setOnCheckedChangeListener(comListener);
        ck_voteResult.setOnCheckedChangeListener(comListener);


        et_notice = findViewById(R.id.et_notice);
        ck_voteMember_1 = findViewById(R.id.ck_voteMember_1);
        ck_voteMember_2 = findViewById(R.id.ck_voteMember_2);
        ck_voteMember_1.setOnClickListener(v -> {
            if (ck_voteMember_1.isChecked()) {
                ck_voteMember_2.setChecked(false);
                voteMember = "true";
            } else {
                if (ck_voteMember_2.isChecked()) {
                    voteMember = "false";
                } else {
                    voteMember = null;
                }
            }
            changeAllCheckStatus();
        });

        ck_voteMember_2.setOnClickListener(v -> {
            if (ck_voteMember_2.isChecked()) {
                ck_voteMember_1.setChecked(false);
                voteMember = "false";
            } else {
                if (ck_voteMember_1.isChecked()) {
                    voteMember = "true";
                } else {
                    voteMember = null;
                }
            }
            changeAllCheckStatus();
        });
        showResult1_1();
    }

    @Override
    public void onClick(View v) {
        mPromptDialog = new CommonPromptDialog(this, CommonPromptDialog.TYPE_TIPS);

        switch (v.getId()) {
            case R.id.btn_vote_create_result:
                if (isReCreate) {
                    reCreate();
                } else {
                    saveData();
                }
                break;
            case R.id.tv_more_voteName:
                mPromptDialog.setTitle("表决事项名称");
                mPromptDialog.setContent(mProVoteDetail.getTitle());
                break;
            case R.id.tv_more_voteType:
                mPromptDialog.setTitle("表决类型");
                mPromptDialog.setContent(ProVote.TYPES.get(mProVoteDetail.getType()));
                break;
            case R.id.tv_more_voteContent:
                mPromptDialog.setTitle("表决内容");
                mPromptDialog.setContent(mProVoteDetail.getContent());
                break;
            case R.id.tv_more_voteRule:
                mPromptDialog.setTitle("表决规则");
                mPromptDialog.setContent(mProVoteDetail.getRuleName());
                break;
            case R.id.tv_more_voteRuleExpls:
                mPromptDialog.setTitle("表决规则说明");
                mPromptDialog.setContent(mProVoteDetail.getRuleExplain());
                break;
            case R.id.tv_more_voteTime:
                mPromptDialog.setTitle("表决时间");
                String strTime = "开始时间：" + DateFormatUtils.long2Str(mProVoteDetail.getStartTime(), true) + "\n";
                strTime = strTime + "结束时间：" + DateFormatUtils.long2Str(mProVoteDetail.getEndTime(), true);
                mPromptDialog.setContent(strTime);
                break;
            case R.id.tv_more_voteDataSta:
                showDataOverview();

                return;
            case R.id.tv_more_voteResult:
                showResult();
//                mPromptDialog.setTitle("表决时间");
//                mPromptDialog.setContent(mProVoteDetail.getre());
                return;
            case R.id.tv_more_voteMember:
                showGetResultMembers();
                break;
            case R.id.tv_all_check:
                boolean isAll =
                        ck_voteName.isChecked() && ck_voteType.isChecked() && ck_voteContent.isChecked() && ck_voteRule.isChecked()
                                && ck_voteRuleExpls.isChecked() && ck_voteTime.isChecked() && ck_voteDataSta.isChecked() && ck_voteResult.isChecked()
                                && !TextUtils.isEmpty(voteMember);

                if (isAll) {
                    tv_all_check.setText("全选");
                    ck_voteName.setChecked(false);
                    ck_voteType.setChecked(false);
                    ck_voteContent.setChecked(false);
                    ck_voteRule.setChecked(false);
                    ck_voteRuleExpls.setChecked(false);
                    ck_voteTime.setChecked(false);
                    ck_voteDataSta.setChecked(false);
                    ck_voteResult.setChecked(false);
                    ck_voteMember_1.setChecked(false);
                    ck_voteMember_2.setChecked(false);
                    voteMember = null;

                } else {
                    tv_all_check.setText("取消全选");
                    ck_voteName.setChecked(true);
                    ck_voteType.setChecked(true);
                    ck_voteContent.setChecked(true);
                    ck_voteRule.setChecked(true);
                    ck_voteRuleExpls.setChecked(true);
                    ck_voteTime.setChecked(true);
                    ck_voteDataSta.setChecked(true);
                    ck_voteResult.setChecked(true);
                    if (TextUtils.isEmpty(voteMember)) {
                        ck_voteMember_1.performClick();
                    }
                }
                break;
        }
        if (!TextUtils.isEmpty(mPromptDialog.getTitle())) {
            mPromptDialog.show();
        }

    }

    private JsonObject resultData = null;

    private void showResult() {
        if (resultData == null) {
            Network.getAPIService()
                    .getVoteResulttatus_Manager(getUserToken(), mProVoteDetail.getId())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseSubscriber<JsonObject>() {
                        @Override
                        public void onNext(JsonObject json) {
                            hideLoading();
                            int code = json.get("code").getAsInt();
                            if (code != 0) {
                                super.onNext(json);
                                return;
                            }
                            resultData = json.getAsJsonObject("data");
                            showResult2();
                        }

                        @Override
                        public void onCompleted() {
                            hideLoading();
                            super.onCompleted();
                        }

                        @Override
                        public void onError(Throwable e) {
                            hideLoading();
                            super.onError(e);
                        }
                    });
        } else {
            showResult2();
        }
    }

    private void showResult1_1() {
        if (resultData == null) {
            Network.getAPIService()
                    .getVoteResulttatus_Manager(getUserToken(), mProVoteDetail.getId())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseSubscriber<JsonObject>() {
                        @Override
                        public void onNext(JsonObject json) {
                            hideLoading();
                            int code = json.get("code").getAsInt();
                            if (code != 0) {
                                super.onNext(json);
                                return;
                            }
                            resultData = json.getAsJsonObject("data");
                            boolean result = resultData.get("status").getAsBoolean();
                            String statusStr = "";
//        if(mProVoteDetail.getStatus() == 5 || mProVoteDetail.getStatus() == 6)
//        {
                            if (result) {
                                statusStr = "通过。";
                            } else {
                                statusStr = "未通过。";
                            }
//            et_notice.setVisibility(View.GONE);
                            if (mProVoteDetail.getRule() == 0) {


                                et_notice.setText("根据《中华人民共和国企业破产法》第六十四条，该表决事项" + statusStr);
                            } else if (mProVoteDetail.getRule() == 1) {
                                et_notice.setText(
                                        "根据《中华人民共和国企业破产法》第九十七条，该表决事项" + statusStr);
                            } else if (mProVoteDetail.getRule() == 2) {
                                et_notice.setText(
                                        "根据《中华人民共和国企业破产法》第八十四条、第八十六条，该表决事项" + statusStr);
                            } else if (mProVoteDetail.getRule() == 3) {
                                et_notice.setText("根据《最高人民法院关于适用<中华人民共和国企业破产法>若干问题的规定（三）》第十四条，该表决事项" + statusStr);
                            }
                        }

                        @Override
                        public void onCompleted() {
                            hideLoading();
                            super.onCompleted();
                        }

                        @Override
                        public void onError(Throwable e) {
                            hideLoading();
                            super.onError(e);
                        }
                    });
        } else {
            showResult2();
        }
    }

    private void showResult2() {
        try {
            boolean result = resultData.get("status").getAsBoolean();
            mPromptDialog.setTitle("表决结果");
            mPromptDialog.setContent(result ? "已通过" : "未通过");
            mPromptDialog.show();
        } catch (Exception ex) {
            ToastUtil.showShort("数据错误");
        }
    }

    private void showGetResultMembers() {
        int type = 1;
        if (ck_voteMember_2.isChecked()) {
            type = 2;
        }

        showLoading("获取中...");
        int finalType = type;
        Network.getAPIService()
                .getResultMembers(getUserToken(), mProVoteDetail.getId(), mProVoteDetail.getBankruptcyProjectId(), type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        hideLoading();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }
                        JsonObject data = json.getAsJsonObject("data");
                        if (finalType == 1) {
                            String names = "";
                            JsonArray nameArr = data.getAsJsonArray("names");
                            for (int i = 0; i < nameArr.size(); i++) {
                                names = names + nameArr.get(i).getAsJsonObject().get("member").getAsString();
                                if (i < nameArr.size() - 1) {
                                    names = names + ",";
                                }
                            }
                            View v = View.inflate(ProVoteCreateResultActivity.this, R.layout.overview_voters_names,
                                    null);
                            ((TextView) v.findViewById(R.id.tv_names)).setText(names);

                            mOverviewDialog = new VoteOverviewPromptDialog(ProVoteCreateResultActivity.this);
                            mOverviewDialog.show();
                            mOverviewDialog.area_overview.addView(v);
                        } else if (finalType == 2) {
                            mOverviewDialog = new VoteOverviewPromptDialog(ProVoteCreateResultActivity.this);
                            mOverviewDialog.show();
                            if (data.get("groupInfo") != null) {
                                JsonArray arr = data.getAsJsonArray("groupInfo");
                                for (int i = 0; i < arr.size(); i++) {
                                    mOverviewDialog.area_overview.addView(createOverVotersviewItem(arr.get(i).getAsJsonObject(), mProVoteDetail.isDefaultAgreed(),
                                            true));
                                }
                            } else {
                                mOverviewDialog.area_overview.addView(createOverVotersviewItem(data,
                                        mProVoteDetail.isDefaultAgreed(), false));
                            }

                        }
                    }

                    @Override
                    public void onCompleted() {
                        hideLoading();
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        super.onError(e);
                    }
                });
    }

    private JsonObject overData = null;

    private void showDataOverview() {
        if (overData == null) {
            showLoading("获取中...");
            Network.getAPIService()
                    .getVoteStatisticsOverview_Manager(getUserToken(), mProVoteDetail.getId())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new BaseSubscriber<JsonObject>() {
                        @Override
                        public void onNext(JsonObject json) {
                            hideLoading();
                            int code = json.get("code").getAsInt();
                            if (code != 0) {
                                super.onNext(json);
                                return;
                            }
                            overData = json.getAsJsonObject("data");
                            showDataOverview2();
                        }

                        @Override
                        public void onCompleted() {
                            hideLoading();
                            super.onCompleted();
                        }

                        @Override
                        public void onError(Throwable e) {
                            hideLoading();
                            super.onError(e);
                        }
                    });
        } else {
            showDataOverview2();
        }
    }

    private VoteOverviewPromptDialog mOverviewDialog;

    private void showDataOverview2() {
        mOverviewDialog = new VoteOverviewPromptDialog(this);
        mOverviewDialog.show();

        if (overData.get("groupsList") != null) {
            boolean defaultAgreed = overData.get("defaultAgreed").getAsBoolean();
            JsonArray arr = overData.getAsJsonArray("groupsList");
            for (int i = 0; i < arr.size(); i++) {
                mOverviewDialog.area_overview.addView(createOverviewItem(arr.get(i).getAsJsonObject(), defaultAgreed,
                        true));
            }
        } else {
            boolean defaultAgreed = overData.get("defaultAgreed").getAsBoolean();
            mOverviewDialog.area_overview.addView(createOverviewItem(overData, defaultAgreed, false));
        }
    }

    private View createOverVotersviewItem(JsonObject object, boolean defaultAgreed, boolean isGroup) {
        View v = View.inflate(this, R.layout.overview_voters_item, null);

        if (isGroup) {
            ((TextView) v.findViewById(R.id.tv_group_name)).setText(object.get("groupName").getAsString());
        } else {
            ((TextView) v.findViewById(R.id.tv_group_name)).setVisibility(View.GONE);
        }


        try {

            String yibiaojue_names = "";
            JsonArray agreedMemsNameArr = object.getAsJsonArray("agreedMemsName");
            for (int i = 0; i < agreedMemsNameArr.size(); i++) {
                if (!TextUtils.isEmpty(yibiaojue_names)) {
                    yibiaojue_names = yibiaojue_names + ",";
                }
                yibiaojue_names = yibiaojue_names + agreedMemsNameArr.get(i).getAsString();
            }

            JsonArray disAgreedMemsNameArr = object.getAsJsonArray("disAgreedMemsName");
            for (int i = 0; i < disAgreedMemsNameArr.size(); i++) {
                if (!TextUtils.isEmpty(yibiaojue_names)) {
                    yibiaojue_names = yibiaojue_names + ",";
                }
                yibiaojue_names = yibiaojue_names + disAgreedMemsNameArr.get(i).getAsString();
            }

            String weicanhui_names = "";
            JsonArray notAttendMemsNameArr = object.getAsJsonArray("notAttendMemsName");
            for (int i = 0; i < notAttendMemsNameArr.size(); i++) {
                if (!TextUtils.isEmpty(weicanhui_names)) {
                    weicanhui_names = weicanhui_names + ",";
                }
                weicanhui_names = weicanhui_names + notAttendMemsNameArr.get(i).getAsString();
            }

            ((TextView) v.findViewById(R.id.tv_totalMembers)).setText(object.get("totalMembers").getAsInt() + "人");
            ((TextView) v.findViewById(R.id.tv_yibiaojue_num)).setText((object.get("agreedMembers").getAsInt() + object.get("disAgreedMembers").getAsInt()) + "人");
            ((TextView) v.findViewById(R.id.tv_yibiaojue_names)).setText(yibiaojue_names);

            ((TextView) v.findViewById(R.id.tv_weicanhui_num)).setText(object.get("notAttendMembers").getAsInt() + "人");
            ((TextView) v.findViewById(R.id.tv_weicanhui_names)).setText(weicanhui_names);

            if (defaultAgreed) {
                v.findViewById(R.id.area_canhuiweibiaojue).setVisibility(View.GONE);
            } else {
                String canhuiweibiaojue_names = "";
                JsonArray notVoteMemsNameArr = object.getAsJsonArray("notVoteMemsName");
                for (int i = 0; i < notVoteMemsNameArr.size(); i++) {
                    if (!TextUtils.isEmpty(canhuiweibiaojue_names)) {
                        canhuiweibiaojue_names = canhuiweibiaojue_names + ",";
                    }
                    canhuiweibiaojue_names = canhuiweibiaojue_names + notVoteMemsNameArr.get(i).getAsString();
                }

                ((TextView) v.findViewById(R.id.tv_canhuiweibiaojue_num)).setText(object.get("notVoteMembers").getAsInt() + "人");
                ((TextView) v.findViewById(R.id.tv_canhuiweibiaojue_names)).setText(canhuiweibiaojue_names);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return v;
    }


    private View createOverviewItem(JsonObject object, boolean defaultAgreed, boolean isGroup) {
        NumberFormat percentFormat = NumberFormat.getPercentInstance();
        percentFormat.setMaximumFractionDigits(2); //最大小数位数
        percentFormat.setMinimumFractionDigits(2); //最小小数位数

        View v = View.inflate(this, R.layout.overview_vote_item, null);

        if (isGroup) {
            ((TextView) v.findViewById(R.id.tv_group_name)).setText(object.get("groupName").getAsString());
        } else {
            ((TextView) v.findViewById(R.id.tv_group_name)).setVisibility(View.GONE);
        }

        try {

            ((TextView) v.findViewById(R.id.tv_vote_rate_totalMembers)).setText(object.get("totalMeetingMember").getAsInt() + "人");
            ((TextView) v.findViewById(R.id.pro_vote_agree_number)).setText(object.get("agreedNum").getAsInt() + "人");
            ((TextView) v.findViewById(R.id.pro_vote_agree_percent)).setText("占比：" + percentFormat.format(object.get(
                    "agreedVoterRate").getAsDouble()));
            ((TextView) v.findViewById(R.id.pro_vote_not_agree_number)).setText(object.get("disAgreedNum").getAsInt() + "人");
            ((TextView) v.findViewById(R.id.pro_vote_not_agree_percent)).setText("占比：" + percentFormat.format(object.get("disAgreedVoterRate").getAsDouble()));

            ((TextView) v.findViewById(R.id.pro_vote_not_join_number)).setText(object.get("notAttendNum").getAsInt() + "人");
            ((TextView) v.findViewById(R.id.pro_vote_not_join_percent)).setText("占比：" + percentFormat.format(object.get("notAtdVoterRate").getAsDouble()));
//        ((TextView) v.findViewById(R.id.tv_vote_rate_totalMoney)).setText(percentFormat.format(object.get
//        ("totalAmount").getAsDouble()) + "元");
            ((TextView) v.findViewById(R.id.tv_vote_rate_totalMoney)).setText(object.get("totalAmount").getAsDouble() + "元");
            ((TextView) v.findViewById(R.id.pro_vote_money_agree_number)).setText(object.get("agreedAmount").getAsInt() + "元");
            ((TextView) v.findViewById(R.id.pro_vote_money_agree_percent)).setText("占比：" + percentFormat.format(object.get("agreedMoneyRate").getAsDouble()));
            ((TextView) v.findViewById(R.id.pro_vote_money_not_agree_number)).setText(object.get("disAgreedAmount").getAsInt() + "元");
            ((TextView) v.findViewById(R.id.pro_vote_money_not_agree_percent)).setText("占比：" + percentFormat.format(object.get("disAgreedMoneyRate").getAsDouble()));

            ((TextView) v.findViewById(R.id.pro_vote_money_not_join_number)).setText(object.get("notAttendAmount").getAsInt() + "元");
            ((TextView) v.findViewById(R.id.pro_vote_money_not_join_percent)).setText("占比：" + percentFormat.format(object.get("notAtdMoneyRate").getAsDouble()));

            if (defaultAgreed) {
                v.findViewById(R.id.area_not_vote).setVisibility(View.GONE);
                v.findViewById(R.id.area_money_not_vote).setVisibility(View.GONE);
            } else {
                ((TextView) v.findViewById(R.id.pro_vote_not_vote_number)).setText(object.get("noVoteNum").getAsInt() + "人");
                ((TextView) v.findViewById(R.id.pro_vote_not_vote_percent)).setText("占比：" + percentFormat.format(object.get("noVoteRate").getAsDouble()));

                ((TextView) v.findViewById(R.id.pro_vote_money_not_vote_number)).setText(object.get("noVoteAmount").getAsInt() + "元");
                ((TextView) v.findViewById(R.id.pro_vote_money_not_vote_percent)).setText("占比：" + percentFormat.format(object.get("noVoteMoneyRate").getAsDouble()));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }


        return v;

    }


    public void saveData() {
        String notice = et_notice.getText().toString().trim();

        showLoading("提交中...");
        Network.getAPIService()
                .createVoteResult_Manager(getUserToken(), mProVoteDetail.getId(), ck_voteName.isChecked(),
                        ck_voteType.isChecked(), ck_voteContent.isChecked(), ck_voteRule.isChecked(),
                        ck_voteRuleExpls.isChecked(), ck_voteTime.isChecked(), ck_voteDataSta.isChecked(),
                        ck_voteResult.isChecked(), voteMember, notice)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        hideLoading();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }
                        ProVoteResult result = ProVoteResult.parse(json.getAsJsonObject("data"));
                        Intent intent = new Intent(ProVoteCreateResultActivity.this, ProVoteResultFileActivity.class);
                        intent.putExtra(ProVoteResultFileActivity.KEY_VOTE, mProVoteDetail);
                        intent.putExtra(ProVoteResultFileActivity.KEY_RESULT, result);
                        intent.putExtra(KEY_PROJECTID, mProVoteDetail.getBankruptcyProjectId());
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        hideLoading();
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        super.onError(e);
                    }
                });
    }

    private void reCreate() {
        String notice = et_notice.getText().toString().trim();

        if (mProVoteResult == null) {
            return;
        }
        final String reid = mProVoteResult.getResultId();

        showLoading("提交中...");
        Network.getAPIService()
                .reCreateVoteResult_Manager(getUserToken(), mProVoteDetail.getId(), ck_voteName.isChecked(),
                        ck_voteType.isChecked(), ck_voteContent.isChecked(), ck_voteRule.isChecked(),
                        ck_voteRuleExpls.isChecked(), ck_voteTime.isChecked(), ck_voteDataSta.isChecked(),
                        ck_voteResult.isChecked(), voteMember, notice, mProVoteResult.getResultId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<JsonObject>() {
                    @Override
                    public void onNext(JsonObject json) {
                        hideLoading();
                        int code = json.get("code").getAsInt();
                        if (code != 0) {
                            super.onNext(json);
                            return;
                        }
                        ProVoteResult result = ProVoteResult.parse(json.getAsJsonObject("data"));
                        Intent intent = new Intent(ProVoteCreateResultActivity.this, ProVoteResultFileActivity.class);
                        intent.putExtra(ProVoteResultFileActivity.KEY_VOTE, mProVoteDetail);
//                        result.setResultId(reid);
                        intent.putExtra(ProVoteResultFileActivity.KEY_RESULT, result);
                        intent.putExtra(KEY_PROJECTID, mProVoteDetail.getBankruptcyProjectId());
                        startActivity(intent);
                        RefreshEvent event = new RefreshEvent();
                        event.setType(RefreshEvent.TYPE_VOTE);
                        EventBus.getDefault().post(event);
                        finish();
                    }

                    @Override
                    public void onCompleted() {
                        hideLoading();
                        super.onCompleted();
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideLoading();
                        super.onError(e);
                    }
                });
    }

}
